package com.neusoft.neuiotms.dcom.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.UserAuthorityConditionConverter;
import com.neusoft.neuiotms.dcom.dto.ShiftDTO;
import com.neusoft.neuiotms.dcom.service.ShiftService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "换班管理")
@RestController
@RequestMapping(value = "/api/w/shifts")
public class ShiftController {

    @Autowired
    private ShiftService shiftService;
    @Autowired
    private UserAuthorityConditionConverter userAuthorityConditionConverter;

    @ApiOperation("查询所有换班")
    @GetMapping
    public ResultListDTO<ShiftDTO> index(final Searchable searchable, final UserBean user) {
        return ResultListDTO.success(this.shiftService.index(searchable));
    }

    @ApiOperation("分页查询所有换班")
    @GetMapping("/p")
    public PageResultDTO<ShiftDTO> search(final Pageable pageable, final Searchable searchable, final UserBean user) {
        this.userAuthorityConditionConverter.addUserCodeAuthoritySearchCondition(searchable, user);
        this.userAuthorityConditionConverter.addUserDownOrgCodesAuthoritySearchCondition(searchable, user);
        return PageResultDTO.success(this.shiftService.search(pageable, searchable));
    }

    @ApiOperation("创建换班")
    @PostMapping("")
    public ResultDTO<ShiftDTO> create(@RequestBody final ShiftDTO dto, final UserBean user) {
        dto.setSourceUserCode(user.getUsername());
        dto.setSourceUserName(user.getName());
        return ResultDTO.success(this.shiftService.create(dto));
    }

    @ApiOperation(value = "修改换班")
    @PutMapping("/{id}")
    public ResultDTO<ShiftDTO> update(@PathVariable final Long id,
            @RequestBody final ShiftDTO dto) {
        return ResultDTO.success(this.shiftService.update(id, dto));
    }

    @ApiOperation(value = "确认换班")
    @PostMapping("/{id}/confirm")
    public ResultDTO<ShiftDTO> confirm(@PathVariable final Long id) {
        return ResultDTO.success(this.shiftService.updateStatus(id, true));
    }

    @ApiOperation("换班详情")
    @GetMapping("/{id}")
    public ResultDTO<ShiftDTO> details(@PathVariable final Long id) {
        return ResultDTO.success(this.shiftService.details(id));
    }

    @ApiOperation("删除换班")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.shiftService.delete(id);
        return ResultDTO.success();
    }
}
